<?php

	include_once("Zend/Gdata/ClientLogin.php");
	
	include_once("Zend/Gdata/Calendar.php");

	
	$email = "justin.rao@gmail.com";
	$password = "840804uknmyb";
echo "start to logging in!\n";	
	$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
	$client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, $service);
echo "logged in!\n";

		
	$gdataCal = new Zend_Gdata_Calendar($client);
echo "calendars retrieved!\n";		
	$calFeed = $gdataCal->getCalendarListFeed();

	$startDate = '2008-08-18';
	$endDate = '2008-08-19';
		
	echo  $calFeed->title->text . "\n";
	
	$times = array();
	$titles = array();
	$tags = array();
	foreach ($calFeed as $calendar) {
		$cal_id =  substr($calendar->id->text, strlen('http://www.google.com/calendar/feeds/default/'));
		$titles[$cal_id] = $calendar->title->text;
		$where = $calendar->where;
		if (!empty($where)) {
			echo ($where[0]->getValueString());
			echo ($where[0]->getLabel());
			$tags[$cal_id] =$where[0]->getValueString();
		}
		
	    echo  "\n--- processing calendar {$calendar->title->text}  ===> $cal_id \n";
	    
	  
		$query = $gdataCal->newEventQuery();
  		$query->setUser($cal_id);
  		$query->setVisibility('private');
  		$query->setProjection('full');
  		$query->setOrderby('starttime');
  		$query->setStartMin($startDate);
  		$query->setStartMax($endDate);
  		$eventFeed = $gdataCal->getCalendarEventFeed($query);
    		
  		$time = 0;
  		foreach ($eventFeed as $event) {
    		$when = $event->when[0];
    		$event_start_time = $when->getStartTime();
    		$event_end_time = $when->getEndTime();
    		
    		echo "{$event->title} : {$event_start_time} -- {$event_end_time} \n";
    		
    		$start_time = date_parse($when->getStartTime());
    		$end_time = date_parse($when->getEndTime());
    		
    		$duration = ($end_time['hour'] - $start_time['hour']) * 60 
    						+ $end_time['minute'] - $start_time['minute'];
    						
    						
    		$time += $duration;
    		
  		}	
  		
  		$times[$cal_id] = $time;
	}
	
	
	foreach ($titles as $cal_id => $title) {
		if ($times[$cal_id] == 0) {
			continue;
		}
		echo "$title : {$times[$cal_id]} mins : tag:{$tags[$cal_id]}\n";
	}
	  
?>
